﻿using PesticideManage.API.Services.Identity.RoleAggregate;
using PesticideManage.API.Services.Identity.UserAggregate;
using PesticideManage.API.Services.Menus;

namespace PesticideManage.API.Data;

public static class SeedData
{
    public static void Initialize(IServiceProvider serviceProvider)
    {
        var context = serviceProvider.GetRequiredService<PesticideContext>();
        context.Database.EnsureDeleted();//
        context.Database.EnsureCreated();
        // context.Database.Migrate();

        if (context == null || context.Pesticides == null)
        {
            throw new ArgumentNullException("Null RazorPagesMovieContext");
        }
        if (context.Users.Any())
        {
            return;
        }
        Role defaultRole = new()
        {
            Name = "default",
            Menus =
            new Menu[]{
                new (){Order =1, Name = "产品",Meta= new(){Icon="GoodsFilled"} },
                new (){Order =2,Name = "二维码",Meta= new(){Icon="Grid"} },
                new ()
                {
                    Order =3,
                    Name = "账户管理",

                    Meta= new(){Icon="Avatar"},
                    Children =
                    new Menu[]{
                        new (){Order = 1,Name = "个人中心"},
                        //new (){Order = 2,Name = "实名认证"},
                        new (){Order = 2,Name = "企业信息" },
                    }
                },
            }
        };
        Role admin = new()
        {
            Name = "admin",
            Menus =
            new Menu[]{
                new ()
                {
                    Order=4,
                    Name = "系统设置",

                    Meta= new(){Icon="Setting"} ,
                    Children =
                    new Menu[]
                    {
                        new (){Order = 1,Name = "系统配置"},
                        new (){Order = 2,Name = "用户管理"},
                        new (){Order = 3,Name = "权限管理",
                        Children = new Menu[]
                        {
                            new(){Name = "身份管理",Meta = new()},
                            new(){Name = "菜单管理",Meta = new()}
                        }
                    },
                },

            }
            }
        };
        User user = new()
        {
            Name = "admin",
            // Password = "123456",
        };
        user.SetPassword("123456");
        user.Roles = new[] { admin, defaultRole };
        context.Users.Add(user);
        context.SaveChanges();
    }
}
